package com.lbn.baseframe.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.test.UiThreadTest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lbn.baseframe.mvp.BasePresenter;
import com.lbn.baseframe.mvp.IMvp;
import com.lbn.baseframe.ui.IAppbar;
import com.lbn.baseframe.utils.EasyToast;
import com.lbn.baseframe.utils.L;
import com.lbn.baseframe.utils.LoadingUtils;
import com.lypeer.fcpermission.ui.FcPermissionsFragment;

import java.util.List;

/**
 * @author 李炳南
 * @date 2017/2/8
 */
public class BaseFragment<P extends BasePresenter> extends FcPermissionsFragment implements IBase, IMvp {

    protected P mPresenter;

    protected View rootView;


    @Deprecated
    @Override
    public void setAppbar(IAppbar appbar) {
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        L.e("onViewCreated:" + this.getClass().toString());
        L.e("rootView:" + (rootView == null));
        L.e("view:" + (view == null));
        rootView = view;
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }

        if (null != mPresenter) {
            mPresenter.attachView(this);
            onPresenterCreated();
        }
    }


    /**
     * 创建Presenter
     */
    @Override
    public P createPresenter() {
        return null;
    }

    /**
     * 创建Presenter完成
     */
    protected void onPresenterCreated() {
    }


    @Override
    public void onDestroyView() {
        if (mPresenter != null) {
            mPresenter.destroy();
        }
        super.onDestroyView();
    }

    @NonNull
    @Override
    protected String getRationale4NeverAskAgain() {
        return null;
    }

    @Override
    public void onPermissionDenied(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onError(String error) {
        EasyToast.showShort(getContext(),error);
    }

    @Override
    public void showLoading() {
        LoadingUtils.showDialog(getFragmentManager());
    }

    @Override
    public void hideLoading() {
        LoadingUtils.dismiss(getFragmentManager());
    }

}
